import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// !#2 新建仓库并导出
// 参数：仓库名（唯一）、配置
/* const useCounterStore = defineStore('counter', () => {
  // 业务逻辑，可以理解为这儿写的代码，相当于在 setup 中
  // !state
  const count = ref(0)
  // !action
  const addCount = () => {
    count.value++
  }
  const addCountAsync = () => {
    setTimeout(() => {
      count.value++
    }, 2000)
  }
  // !getters
  const doubleCount = computed(() => count.value * 2)
  return { count, addCount, addCountAsync, doubleCount }
}) */

// !选项式的写法
const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      count: 1
    }
  },
  actions: {
    // num 是触发这个 action 的时候，外界传递过来的
    addCount(num: number) {
      this.count += 1
    },
    addCountAsync() {
      setTimeout(() => {
        this.count += 1
      }, 2000)
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2
  }
})

export default useCounterStore